package com.gywj.race.service;

import com.google.common.collect.Lists;
import com.gywj.race.config.dao.Assembler;
import com.gywj.race.config.exception.BaseException;
import com.gywj.race.config.page.PageParam;
import com.gywj.race.config.page.PageResult;
import com.gywj.race.dto.EventGroupAddDto;
import com.gywj.race.dto.EventInformationAddDto;
import com.gywj.race.dto.EventInformationUpdateDto;
import com.gywj.race.dto.EventPriceAddDto;
import com.gywj.race.entity.*;
import com.gywj.race.mapper.EventGroupMapper;
import com.gywj.race.mapper.EventInformationMapper;
import com.gywj.race.mapper.EventPriceMapper;
import com.gywj.race.mapper.OrganizerMapper;
import com.gywj.race.util.ObjectUtil;
import org.jacoco.agent.rt.internal_43f5073.core.internal.flow.IFrame;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * The type Event service.
 */
@Service
public class EventService {
    @Resource
    private EventInformationMapper eventInformationMapper;
    @Resource
    private EventPriceMapper eventPriceMapper;
    @Resource
    private EventGroupMapper eventGroupMapper;
    @Resource
    private OrganizerMapper organizerMapper;

    /**
     * 添加赛事组别
     *
     * @param eventGroupDto the event group dto
     */
    public void addGroup(EventGroupAddDto eventGroupDto,HttpServletRequest request) {
        try {
            EventGroup eventGroup = new EventGroup();
            Assembler.assemble(eventGroupDto, eventGroup);
            eventGroup.setCreateTime(new Date());
            eventGroup.setUpdateTime(new Date());
            eventGroup.setStatus(1);
            eventGroup.setCurrentMax(0);
            Employees employee = (Employees) request.getSession().getAttribute("employee");
            eventGroup.setCreateEmployee(employee.getId());
            eventGroup.setUpdateEmployee(employee.getId());
            eventGroupMapper.insertSelective(eventGroup);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage());
        }
    }

    /**
     * 获取组别列表 下拉框
     *
     * @return the group list
     */
    public List<EventGroup> getGroupList(Integer eventId) {
        try {
            EventGroupExample eventGroupExample = new EventGroupExample();
            eventGroupExample.createCriteria().andBelongEventIdEqualTo(eventId);
            List<EventGroup> eventGroups = eventGroupMapper.selectByExample(eventGroupExample);
            return eventGroups;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage());
        }
    }

    /**
     * 获取组别列表 分页
     *
     * @param pageParam the page param
     * @return the group list page
     */
    public PageResult getGroupListPage(PageParam pageParam) {
        try {
            PageResult pageResult = new PageResult();
            pageResult.setPage(pageParam.getPage());
            pageResult.setLimit(pageParam.getLimit());
            EventGroupExample eventGroupExample = new EventGroupExample();
            eventGroupExample.setPageNum(pageParam.getPage()).setPageSize(pageParam.getLimit());
            int count = eventGroupMapper.countByExample(eventGroupExample);
            if (0 == count) {
                pageResult.setCount(0);
                pageResult.setList(Lists.newArrayList());
                return pageResult;
            }
            List<EventGroup> eventGroups = eventGroupMapper.selectByExample(eventGroupExample);
            pageResult.setCount(count);
            pageResult.setList(eventGroups);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage());
        }
    }

    /**
     * 添加赛事套餐
     *
     * @param eventPriceDto the event price dto
     */
    public void addPrice(EventPriceAddDto eventPriceDto) {
        try {
            EventPrice eventPrice = new EventPrice();
            Assembler.assemble(eventPriceDto, eventPrice);
            eventPriceMapper.insertSelective(eventPrice);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage());
        }
    }

    /**
     * 获取赛事套餐列表 - 下拉框
     *
     * @return the price list
     */
    public List<EventPrice> getPriceList(Integer eventId) {
        try {
            EventPriceExample eventPriceExample = new EventPriceExample();
            eventPriceExample.createCriteria().andBelongEvevtIdEqualTo(eventId);
            List<EventPrice> eventPrice = eventPriceMapper.selectByExample(eventPriceExample);
            return eventPrice;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage());
        }
    }

    /**
     * 获取赛事套餐列表 - 分页
     *
     * @param pageParam the page param
     * @return the price list page
     */
    public PageResult getPriceListPage(PageParam pageParam) {
        try {
            PageResult pageResult = new PageResult();
            pageResult.setPage(pageParam.getPage());
            pageResult.setLimit(pageParam.getLimit());
            EventPriceExample eventPriceExample = new EventPriceExample();
            eventPriceExample.setPageNum(pageParam.getPage()).setPageSize(pageParam.getLimit());
            int count = eventPriceMapper.countByExample(eventPriceExample);
            if (0 == count) {
                pageResult.setCount(0);
                pageResult.setList(Lists.newArrayList());
                return pageResult;
            }
            List<EventPrice> eventPrice = eventPriceMapper.selectByExample(eventPriceExample);
            pageResult.setCount(count);
            pageResult.setList(eventPrice);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage());
        }
    }

    /**
     * 添加赛事信息
     *
     * @param eventInformationDto the event information dto
     */
    public void addInformation(EventInformationAddDto eventInformationDto, HttpServletRequest request) {
        try {
            EventInformation eventInformation = new EventInformation();
            Assembler.assemble(eventInformationDto, eventInformation);
            eventInformation.setCollectionCount(0);
            eventInformation.setShareCount(0);
            eventInformation.setViewCount(0);
            eventInformation.setCreateTime(new Date());
            eventInformation.setUpdateTime(new Date());
            Employees employee = (Employees) request.getSession().getAttribute("employee");
            eventInformation.setCreateEmployee(employee.getId());
            eventInformation.setUpdateEmployee(employee.getId());
            eventInformationMapper.insertSelective(eventInformation);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage());
        }
    }

    public void updateInformation(EventInformation eventInformation) {
        try {
            eventInformation.setUpdateEmployee(1);
            eventInformation.setUpdateTime(new Date());
            eventInformationMapper.updateByPrimaryKeySelective(eventInformation);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage());
        }
    }


    public void deleteEventById(Integer eventId) {
        try {
            eventInformationMapper.deleteByPrimaryKey(eventId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage());
        }
    }

    public void deleteEventPriceById(Integer priceId) {
        try {
            eventPriceMapper.deleteByPrimaryKey(priceId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage());
        }
    }

    public void deleteEventGroupById(Integer groupId) {
        try {
            eventGroupMapper.deleteByPrimaryKey(groupId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage());
        }
    }


    /**
     * 获取赛事信息列表 下拉框
     *
     * @return the information list
     */
    public List<EventInformation> getInformationList() {
        try {
            List<EventInformation> eventInformations = eventInformationMapper.selectByExampleWithBLOBs(new EventInformationExample());
            return eventInformations;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage());
        }
    }

    /**
     * 获取赛事信息列表 分页
     *
     * @param pageParam the page param
     * @return the information list page
     */
    public PageResult getInformationListPage(PageParam pageParam) {
        try {
            PageResult pageResult = new PageResult();
            pageResult.setPage(pageParam.getPage());
            pageResult.setLimit(pageParam.getLimit());
            EventInformationExample eventInformationExample = new EventInformationExample();
            eventInformationExample.setPageNum(pageParam.getPage()).setPageSize(pageParam.getLimit());
            int count = eventInformationMapper.countByExample(eventInformationExample);
            if (0 == count) {
                pageResult.setCount(0);
                pageResult.setList(Lists.newArrayList());
                return pageResult;
            }
            List<EventInformation> eventInformations = eventInformationMapper.selectByExampleWithBLOBs(eventInformationExample);
            pageResult.setCount(count);
            pageResult.setList(eventInformations);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage());
        }
    }

    /**
     * 获取赛事信息列表 下拉框
     *
     * @return the organizer list
     */
    public List<Organizer> getOrganizerList() {
        try {
            List<Organizer> organizers = organizerMapper.selectByExample(new OrganizerExample());
            return organizers;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage());
        }
    }


    public Organizer getOrganizerById(Integer id){
        try {
            Organizer o = organizerMapper.selectByPrimaryKey(id);
            if (ObjectUtil.isNullObj(o)){
                throw new BaseException("未找到数据");
            }
            return o;
        }catch (Exception e){
            e.printStackTrace();
            throw new BaseException(e.getMessage());
        }
    }


    public EventInformation getEventInformationById(Integer id) {

        try {
            EventInformation eventInformation = eventInformationMapper.selectByPrimaryKey(id);
            if (ObjectUtil.isNullObj(eventInformation)) {
                throw new BaseException("未找到数据");
            }
            return eventInformation;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage());
        }
    }

    public void getEventInformationIsAble(Integer id) {
        EventInformation eventInformationById = getEventInformationById(id);
        long partEndTime = eventInformationById.getPartEndTime().getTime();
        Date today = new Date();
        long todayTime = today.getTime();
        if (todayTime>partEndTime){
            throw new BaseException("该赛事报名已结束！");
        }
        long raceEndtime = eventInformationById.getRaceEndTime().getTime();
        if (todayTime>raceEndtime){
            throw new BaseException("该赛事比赛已结束！");
        }
    }
}
